﻿using System.IO;
using System.Text;

namespace Pagination.RangeCollector
{
    public class StreamRangeCollector : IRangeCollector
    {
        private readonly StreamWriter writer;
        private bool isFirst;

        public StreamRangeCollector(Stream stream)
        {
            writer = new StreamWriter(stream, Encoding.UTF8, 65535);
            isFirst = true;
        }

        public virtual void Append(int element)
        {
            if(! isFirst)
                writer.Write(' ');
            writer.Write(element);
            isFirst = false;
        }

        public virtual void End()
        {
            isFirst = true;
            writer.WriteLine();
        }

        public virtual void Dispose()
        {
            writer.Close();
        }
    }
}
